package com.loyal.loyalerp.humenresource.askleave.service.impl;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.humenresource.askleave.mapper.AskLeaveMapper;
import com.loyal.loyalerp.humenresource.askleave.model.AskLeave;
import com.loyal.loyalerp.humenresource.askleave.model.vo.AskLeaveVO;
import com.loyal.loyalerp.humenresource.askleave.model.vo.Check;
import com.loyal.loyalerp.humenresource.askleave.service.AskLeaveService;
import com.loyal.loyalerp.mapper.KaoQinMapper;
import com.loyal.loyalerp.model.KaoQinMessage;
import com.loyal.loyalerp.utils.PropertiesUtil;

@Service
public class AskLeaveServiceImpl implements AskLeaveService {

	@Autowired
	AskLeaveMapper askLeaveMapper;

	@Autowired
	KaoQinMapper kaoqinMapper;

	@Override
	public PageInfo<AskLeave> listAll(Integer PageNum, Integer recordNum) {
		PageHelper.startPage(PageNum, recordNum);
		List<AskLeave> list = askLeaveMapper.selectAll();
		PageInfo<AskLeave> pageInfo = new PageInfo<AskLeave>(list);
		return pageInfo;
	}

	@Override
	public PageInfo<AskLeave> listAllCheck(Integer PageNum, Integer recordNum) {
		PageHelper.startPage(PageNum, recordNum);
		List<AskLeave> list = askLeaveMapper.selectAllCheck();
		PageInfo<AskLeave> pageInfo = new PageInfo<AskLeave>(list);
		return pageInfo;
	}

	@Override
	public PageInfo<AskLeave> listAllNotCheck(Integer PageNum, Integer recordNum) {
		PageHelper.startPage(PageNum, recordNum);
		List<AskLeave> list = askLeaveMapper.selectAllNotCheck();
		PageInfo<AskLeave> pageInfo = new PageInfo<AskLeave>(list);
		return pageInfo;
	}

	@Override
	public PageInfo<AskLeave> listAllCheckAndTime(Integer PageNum, Integer recordNum, String a, String b,
			String personId) {
		PageHelper.startPage(PageNum, recordNum);
		List<AskLeave> list = askLeaveMapper.selectAllCheckAndTime(a, b, personId);
		//请假审批管理图片显示
		String urlRoot = PropertiesUtil.getValue("urlRoot");
		for (AskLeave askLeave : list) {
			String picture = askLeave.getPicture();
			if (picture!=null && !picture.equals("")) {
				askLeave.setPicture(urlRoot + picture);
			}
			String bak1 = askLeave.getBak1();
			if (bak1!=null && !bak1.equals("")) {
				askLeave.setBak1(urlRoot + bak1);
			}
			String bak2 = askLeave.getBak2();
			if (bak2!=null && !bak2.equals("")) {
				askLeave.setBak2(urlRoot + bak2);
			}
		}
		
		PageInfo<AskLeave> pageInfo = new PageInfo<AskLeave>(list);
		return pageInfo;
	}

	@Override
	public PageInfo<AskLeave> listAllNotCheckAndTime(Integer PageNum, Integer recordNum, String a, String b,
			String personId) {
		PageHelper.startPage(PageNum, recordNum);
		List<AskLeave> list = askLeaveMapper.selectAllNotCheckAndTime(a, b, personId);
		//请假审批管理图片显示
				String urlRoot = PropertiesUtil.getValue("urlRoot");
				for (AskLeave askLeave : list) {
					String picture = askLeave.getPicture();
					if (picture!=null && !picture.equals("")) {
						askLeave.setPicture(urlRoot + picture);
					}
					String bak1 = askLeave.getBak1();
					if (bak1!=null && !bak1.equals("")) {
						askLeave.setBak1(urlRoot + bak1);
					}
					String bak2 = askLeave.getBak2();
					if (bak2!=null && !bak2.equals("")) {
						askLeave.setBak2(urlRoot + bak2);
					}
				}
		
		PageInfo<AskLeave> pageInfo = new PageInfo<AskLeave>(list);
		return pageInfo;
	}

	@Override
	public PageInfo<AskLeave> listCurrentUser(Integer id, Integer PageNum, Integer recordNum) {
		PageHelper.startPage(PageNum, recordNum);
		List<AskLeave> list = askLeaveMapper.selectCurrentUserAskLeave(id);
		PageInfo<AskLeave> pageInfo = new PageInfo<AskLeave>(list);
		return pageInfo;
	}
	
	@Override
	public PageInfo<AskLeave> listCurrentUserInput(Integer PageNum, Integer recordNum) {
		PageHelper.startPage(PageNum, recordNum);
		List<AskLeave> list = askLeaveMapper.selectCurrentUserAskLeaveInput();

		String urlRoot = PropertiesUtil.getValue("urlRoot");
		for (AskLeave askLeave : list) {
			String picture = askLeave.getPicture();
			if (picture!=null && !picture.equals("")) {
				askLeave.setPicture(urlRoot + picture);
			}
			String bak1 = askLeave.getBak1();
			if (bak1!=null && !bak1.equals("")) {
				askLeave.setBak1(urlRoot + bak1);
			}
			String bak2 = askLeave.getBak2();
			if (bak2!=null && !bak2.equals("")) {
				askLeave.setBak2(urlRoot + bak2);
			}
		}
		PageInfo<AskLeave> pageInfo = new PageInfo<AskLeave>(list);
		//从upload_img目录下读取图片
		
		return pageInfo;
	}

	@Override
	public int delAskLeave(Integer id) {
		// TODO 自动生成的方法存根
		return askLeaveMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PageInfo<AskLeave> findAskLeave(Integer PageNum, Integer recordNum, String sord, String searchField,
			String searchString) {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public boolean addAskLeave(AskLeave askLeave) {
		return askLeaveMapper.insert(askLeave) != 0;
	}

	@Override
	public boolean kaoQinMonthHasExist(Integer year, Integer month, Integer id) {
		return askLeaveMapper.selectKaoQinMonthHasExist(year, month, id) != 0;
	}

	@Override
	public boolean editAskLeave(AskLeave askLeave) {
		return askLeaveMapper.updateByPrimaryKeySelective(askLeave) != 0;
	}

	@Override
	public boolean hasCheck(Integer id) {

		AskLeave askLeave = askLeaveMapper.selectByPrimaryKey(id);
		if (askLeave != null && askLeave.getCheckerId() != null && askLeave.getCheckerId().intValue() != 0) {
			return true;
		}
		return false;
	}

	@Override
	public AskLeave askLeaveService(Integer id) {
		// TODO Auto-generated method stub
		return askLeaveMapper.selectByPrimaryKey(id);
	}

	@Override
	public String updateKaoQin(Check check) {
		String type = check.getType();
		KaoQinMessage message = kaoqinMapper.selectInitDataByPersonIdAndYears(check);
		if (type.equals("年假")) {
			// 判断年假是否大于-5
			if ((message.getHolidaysEnd() - check.getDays()) < -5) {
				return "年假不足";
			} else {
				kaoqinMapper.updateKaoQinDataToYear(check);
				return "审批成功";
			}

		}
		if (type.equals("事假")) {

			kaoqinMapper.updateKaoQinDataToPerson(check);
			return "审批成功";

		}
		
		
		
		if (type.equals("病假")) {
			float totalSick = message.getSickDaysTotal();
			if (totalSick > 5) {
				  System.out.println("请假天数"+check.getDays()*0.4f);
                  check.setSickDays(check.getDays()*0.4f);
                  kaoqinMapper.updateKaoQinDataToSick(check);
			} else {
				if (totalSick + check.getDays() > 5) {
					check.setSickDays((float) ((check.getDays() + totalSick - 5) * 0.4f));
					kaoqinMapper.updateKaoQinDataToSick(check);
				} else {
					kaoqinMapper.updateKaoQinDataToSick(check);
				}
			}
		}
		return "";
	}

	@Override
	public String qishenKaoQin(Check check) {
		String type = check.getType();
		KaoQinMessage message = kaoqinMapper.selectInitDataByPersonIdAndYears(check);
		if (type.equals("年假")) {
			kaoqinMapper.qishenKaoQinDataToYear(check);
		}
		if (type.equals("事假")) {
			kaoqinMapper.qishenKaoQinDataToPerson(check);
		}
		if (type.equals("病假")) {
			float totalSick = message.getSickDaysTotal();
			if (totalSick - check.getDays() < 5) {
				check.setSickDays((float) check.getDays() - (5 - (totalSick - check.getDays())));
				kaoqinMapper.qishenKaoQinDataToSick(check);
			} else {
				check.setSickDays(check.getDays()*0.4f);
				kaoqinMapper.qishenKaoQinDataToSick(check);
			}
		}
		return "";
	}

	@Override
	public List<AskLeave> selectPersonQinJiaByPhone(String phone) {
		// TODO Auto-generated method stub
		return askLeaveMapper.selectPersonQinJiaByPhone(phone);
	}

	@Override
	public AskLeave selectAskByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return askLeaveMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deleteApplyById(int id) {
		askLeaveMapper.deleteApplyById(id);
		
	}

	@Override
	public List<AskLeave> selectPersonQinJiaByPersonId(int id) {
		// TODO Auto-generated method stub
		return askLeaveMapper.selectPersonQinJiaByPersonId(id);
	}
	
	
	
}
